package com.promise.jokerdream.ui.settings

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.clickable
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.material3.Icon
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.promise.jokerdream.R
import com.promise.jokerdream.ui.components.SettingCard
import com.promise.jokerdream.ui.components.CheckboxOptionRow
import com.promise.jokerdream.ui.components.MultiSelectHashtagBottomSheet
import com.promise.jokerdream.ui.components.SelectionRow
import com.promise.jokerdream.ui.theme.AppSpacing
import com.promise.jokerdream.ui.theme.AppFontSizes
import com.promise.jokerdream.database.entity.KnightGameConfigEntity

/**
 * 侠士客栈设置卡片
 */
@Composable
fun KnightLnnSettingCard(
    config: KnightGameConfigEntity,
    onKnightChange: (Boolean) -> Unit,
    onCollectRewardsChange: (Boolean) -> Unit,
    onExchangeItemsChange: (Boolean) -> Unit,
    onSelectedExchangeItemsChange: (List<String>) -> Unit,
    modifier: Modifier = Modifier,
    onExecuteClick: (() -> Unit)? = null
) {
    var showBottomSheet by remember { mutableStateOf(false) }
    
    SettingCard(
        title = "侠士客栈",
        description = "",
        checked = config.enabledSetting,
        onCheckedChange = onKnightChange,
        modifier = modifier,
        onExecuteClick = onExecuteClick,
        content = {
            if (config.enabledSetting) {
                Column(
                    modifier = Modifier.fillMaxWidth()
                ) {
                    // 领取奖励选项
                    CheckboxOptionRow(
                        title = "领取奖励",
                        checked = config.collectRewardsEnabled,
                        onCheckedChange = onCollectRewardsChange
                    )
                    
                    // 交换物品选项
                    CheckboxOptionRow(
                        title = "交换物品",
                        checked = config.exchangeItemsEnabled,
                        onCheckedChange = onExchangeItemsChange
                    )
                    
                    // 只有选中交换物品时才显示物品选择
                    if (config.exchangeItemsEnabled) {
                        // 物品选择区域
                        val selectedItemsList = remember(config.selectedExchangeItems) {
                            if (config.selectedExchangeItems.isEmpty()) emptyList()
                            else config.selectedExchangeItems.split(",").map { it.trim() }
                        }
                        SelectionRow(
                            label = "选择物品：",
                            selectedText = if (selectedItemsList.isEmpty()) "未选择" else "已选择${selectedItemsList.size}项",
                            onClick = { showBottomSheet = true }
                        )
                    }
                }
            }
        }
    )

    // 物品选择底部弹窗
    if (showBottomSheet) {
        MultiSelectHashtagBottomSheet(
            title = "选择交换物品",
            initialTags = remember(config.selectedExchangeItems) {
                if (config.selectedExchangeItems.isEmpty()) emptyList() else config.selectedExchangeItems.split(",").map { it.trim() }
            },
            placeholder = "输入物品名称，回车添加",
            recommendedTags = getRecommendedKnightItems(),
            onCancel = { showBottomSheet = false },
            onComplete = { items ->
                onSelectedExchangeItemsChange(items)
                showBottomSheet = false
            },
            onDismiss = { showBottomSheet = false }
        )
    }
}

/**
 * 获取推荐的侠士物品列表
 */
private fun getRecommendedKnightItems(): List<String> {
    return listOf(
        "V级万能碎片*1",
        "真黄金卷轴*1",
    )
}
